package team.freeroom.hotelmanage.controller.interceptor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import team.freeroom.hotelmanage.repository.entity.Menu;
import team.freeroom.hotelmanage.util.MenuUtil;
import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONObject;

/**
 * 后台登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) {
    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) {
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object arg2) throws Exception {
        String requestURI = request.getRequestURI();
        Object admin = request.getSession().getAttribute("admin");

        // 表示未登录或者登录失效
        if(admin == null){
            System.out.println("链接"+requestURI+"进入拦截器！");
            String header = request.getHeader("X-Requested-With");
            //判断是否是ajax请求
            if("XMLHttpRequest".equals(header)){
                //表示是ajax请求
                Map<String, String> ret = new HashMap<String, String>();
                ret.put("type", "error");
                ret.put("msg", "登录会话超时或还未登录，请重新登录!");
                response.getWriter().write(JSONObject.fromObject(ret).toString());
                return false;
            }
            else {
                //表示是普通链接跳转，直接重定向到登录页面
                response.sendRedirect(request.getServletContext().getContextPath() + "/system/login");
                return false;
            }
        }
        else {
            //获取菜单id
            String mid = request.getParameter("_mid");
            if (!StringUtils.isEmpty(mid)) {
                List<Menu> allThirdMenu = MenuUtil.getAllThirdMenu((List<Menu>) request.getSession().getAttribute("userMenus"), Long.valueOf(mid));
                request.setAttribute("thirdMenuList", allThirdMenu);
            }
            return true;
        }
    }
}
